package fr.coupedumonde2014.app.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import fr.coupedumonde2014.app.R;
import fr.coupedumonde2014.app.Task.LoadTask;
import fr.coupedumonde2014.app.activity.ListMatch;
import fr.coupedumonde2014.app.adapter.MatchAdapter;
import fr.coupedumonde2014.app.modele.Match;
import fr.coupedumonde2014.app.session.SessionManager;
import fr.coupedumonde2014.app.view.MatchView;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.listeners.HeaderViewListener;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate;

public class ListMatchFragment extends Fragment implements MatchView.DismissCallBack, OnRefreshListener, HeaderViewListener, LoadTask.LoadTaskCallback {

    boolean aVenir = false;

    View view;

    StickyListHeadersListView listMatches;
    List<Match> matches = new ArrayList<Match>();

    ViewGroup layoutPullToRefresh; //la vue prenant en charge le pullToRefresh
    PullToRefreshLayout pullToRefresh; //l'objet qui va gerer le pullToRefresh

    MatchAdapter adapter;

    public class StickyListViewDelegate extends AbsListViewDelegate {
        @Override
        public boolean isReadyForPull(View view, final float x, final float y) {
            StickyListHeadersListView sticky = (StickyListHeadersListView) view;
            return super.isReadyForPull(sticky.getWrappedList(), x, y);
        }
    }

    public ListMatchFragment(){
        this(false);
    }

    public ListMatchFragment(boolean aVenir){
        this.aVenir = aVenir;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.list_match_fragment, container, false);

        if(aVenir)
            this.matches = ListMatch.matchesAVenir;
        else
            this.matches = ListMatch.matchesParis;

        charger();
        remplir();
        ajouterListeners();

        return view;
    }

    public void charger(){
        listMatches = (StickyListHeadersListView) view.findViewById(R.id.listView);

        layoutPullToRefresh = (ViewGroup) view.findViewById(R.id.layout);


    }
    public void remplir(){
        adapter = new MatchAdapter(matches, getActivity().getBaseContext(),this);
        listMatches.setAdapter(adapter);
    }
    public void ajouterListeners(){
        pullToRefresh = new PullToRefreshLayout(getActivity());


        StickyListViewDelegate delegate = new StickyListViewDelegate();
        ActionBarPullToRefresh.from(getActivity())
                .insertLayoutInto(layoutPullToRefresh)
                .theseChildrenArePullable(listMatches)
                .useViewDelegate(StickyListHeadersListView.class, delegate)
                .listener(this)
                .setup(pullToRefresh);

        DefaultHeaderTransformer transformer = (DefaultHeaderTransformer) pullToRefresh
                .getHeaderTransformer();
        transformer.setPullText(getActivity().getString(R.string.rejouer));
        transformer.setRefreshingText(getActivity().getString(R.string.chargement));
        transformer.setProgressBarColor(Color.parseColor("#26aa57"));

        ((TextView)transformer.getHeaderView().findViewById(uk.co.senab.actionbarpulltorefresh.library.R.id.ptr_text)).setTextColor(Color.WHITE);

        pullToRefresh.setHeaderViewListener(this);

    }

    @Override
    public void dismiss(int position) {
        //dismissAdater.animateDismiss(position);

        Match m = (Match)adapter.getItem(position);
        ListMatch.matchesParis.add(m);

        //adapter.remove(position);
        //adapter.notifyDataSetChanged();

        ListMatch.instance.refresh();

    }


    public void refresh() {
        this.adapter.notifyDataSetChanged();
    }

    //Appelle lors de la demande du refresh par le PullToRefresh
    @Override
    public void onRefreshStarted(View view) {
        SessionManager session = new SessionManager(getActivity());

        new LoadTask(this).execute(session.getProfile().getId().toString());
    }

    @Override
    public void onStateChanged(View view, int i) {

    }

    @Override
    public void onLoadTask() {
        pullToRefresh.setRefreshComplete();
        ListMatch.instance.refresh();
    }

    @Override
    public void onLoadTaskErreur() {
        pullToRefresh.setRefreshComplete();
        Toast.makeText(getActivity(), R.string.erreur_reseau, Toast.LENGTH_SHORT).show();
    }
}